/*
 * Copyright (c) 2020 Carnegie Lee
 * All rights reserved
 */
package com.geekc.threadpool.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 线程池配置类属性
 * @author Carnegie Lee
 */
@ConfigurationProperties(prefix = "thread.pool")
public class ThreadPoolConfigurationProperties {
    private int corePoolSize;

    private int maximumPoolSize;

    private long keepAliveTime;

    private TimeUnit timeUnit;

    private String prefixThreadName;

    private RejectedExecutionHandler policy;

    public int getCorePoolSize() {
        return corePoolSize;
    }

    public void setCorePoolSize(int corePoolSize) {
        this.corePoolSize = corePoolSize;
    }

    public int getMaximumPoolSize() {
        return maximumPoolSize;
    }

    public void setMaximumPoolSize(int maximumPoolSize) {
        this.maximumPoolSize = maximumPoolSize;
    }

    public long getKeepAliveTime() {
        return keepAliveTime;
    }

    public void setKeepAliveTime(long keepAliveTime) {
        this.keepAliveTime = keepAliveTime;
    }

    public TimeUnit getTimeUnit() {
        return timeUnit;
    }

    public void setTimeUnit(String timeUnit) {
        switch (timeUnit) {
            case "ns":
                this.timeUnit = TimeUnit.NANOSECONDS;
                break;
            case "us":
                this.timeUnit = TimeUnit.MICROSECONDS;
                break;
            case "ms":
                this.timeUnit = TimeUnit.MILLISECONDS;
                break;
            case "s":
                this.timeUnit = TimeUnit.SECONDS;
                break;
            case "m":
                this.timeUnit = TimeUnit.MINUTES;
                break;
            case "h":
                this.timeUnit = TimeUnit.HOURS;
                break;
            case "d":
                this.timeUnit = TimeUnit.DAYS;
                break;
            default:
                this.timeUnit = null;
        }
    }

    public String getPrefixThreadName() {
        return prefixThreadName;
    }

    public void setPrefixThreadName(String prefixThreadName) {
        this.prefixThreadName = prefixThreadName;
    }

    public RejectedExecutionHandler getPolicy() {
        return policy;
    }

    public void setPolicy(String policy) {
        switch (policy) {
            case "abort":
                this.policy = new ThreadPoolExecutor.AbortPolicy();
                break;
            case "discard":
                this.policy = new ThreadPoolExecutor.DiscardPolicy();
                break;
            case "discardOldest":
                this.policy = new ThreadPoolExecutor.DiscardOldestPolicy();
                break;
            case "caller":
                this.policy = new ThreadPoolExecutor.CallerRunsPolicy();
                break;
            default:
                this.policy = null;
        }
    }
}
